package com.isoftstone.project.app.controller;

import com.isoftstone.common.constant.Constants;
import com.isoftstone.common.utils.CodeUtil;
import com.isoftstone.framework.aop.OpLogAopController;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.PageData;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 总入口
 * @author
 * 修改日期：
 */
@Controller
@Slf4j
@RequestMapping("/app")
public class AppLoginController extends BaseController {

	/**请求登录，验证用户
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/login_login" ,method= RequestMethod.POST,produces="application/json;charset=UTF-8")
	@ResponseBody
	@OpLogAopController(module="APP用户登录",methods="Login",description="")
	public Object login()throws Exception{
		Tidings<String> tidings = new Tidings<String>();
		PageData pd = this.getPageData();
		Subject currentUser = SecurityUtils.getSubject();
		String userName = pd.getString("userName").trim();
		String passWord = pd.getString("passWord").trim();
		//shiro加入身份验证
		UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
		tidings.setStatus(Constants.SUCCESS);

		try {
			currentUser.login(token);
		} catch (UnknownAccountException e1) {
			tidings.setStatus(Constants.ERROR);
			tidings.setMsg("usererror");
		}catch (AuthenticationException e1) {
			tidings.setStatus(Constants.ERROR);
			tidings.setMsg("passerror");
		}
		//验证成功
		return tidings;
	}
	/**
	 * 登陆页面修改密码
	 * @return
	 */
	@RequestMapping(value="/toupdatePassword")
	public ModelAndView toupdatePassword(){
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("user_editPassWord");
		return mv;
	}
}
